﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Searching.SearchEngine;

namespace Searching.UI
{
    public partial class MainForm : Form
    {
        private Search search;

        public MainForm()
        {
            InitializeComponent();
            search = new SimpleSearch();
        }

        private void btnShowAllPages_Click(object sender, EventArgs e)
        {
            StoreForm form = new StoreForm(null);
            form.ShowDialog();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryItem[] items = GetQueryItemsFromDg();

            if (items.Length > 10)
            {
                MessageBox.Show("查询数量过大,最多为10条");
                return;
            }
            //MessageBox.Show(items.ToString());

            StoreForm form = new StoreForm(search.GetReleatedPage(items));
            form.ShowDialog();
        }

        private QueryItem[] GetQueryItemsFromDg()
        {
            DataGridViewRowCollection rows = dgQuery.Rows;
            QueryItem[] result = new QueryItem[rows.Count-1];

            for (int i = 0; i < rows.Count-1; ++i )
            {
                string[] words = new string[5]{"", "", "", "", ""};
                DataGridViewCellCollection cells = rows[i].Cells;

                for (int j = 0; j < cells.Count; ++j )
                {
                    if (cells[j].Value != null)
                    {
                        words[j] = cells[j].Value.ToString();
                    }
                }
                result[i] = new QueryItem(words);
            }
            return result;
        }
    }
}
